package org.jeecg.zhensi.asset.service.impl;

import org.jeecg.zhensi.asset.entity.AssetInventory;
import org.jeecg.zhensi.asset.entity.AssetInventoryZi;
import org.jeecg.zhensi.asset.mapper.AssetInventoryZiMapper;
import org.jeecg.zhensi.asset.mapper.AssetInventoryMapper;
import org.jeecg.zhensi.asset.service.IAssetInventoryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 资产盘点
 * @Author: jeecg-boot
 * @Date:   2023-01-13
 * @Version: V1.0
 */
@Service
public class AssetInventoryServiceImpl extends ServiceImpl<AssetInventoryMapper, AssetInventory> implements IAssetInventoryService {

	@Autowired
	private AssetInventoryMapper assetInventoryMapper;
	@Autowired
	private AssetInventoryZiMapper assetInventoryZiMapper;
	
	@Override
	@Transactional
	public void saveMain(AssetInventory assetInventory, List<AssetInventoryZi> assetInventoryZiList) {
		assetInventoryMapper.insert(assetInventory);
		if(assetInventoryZiList!=null && assetInventoryZiList.size()>0) {
			for(AssetInventoryZi entity:assetInventoryZiList) {
				//外键设置
				entity.setAssetInventoryOrderH(assetInventory.getId());
				assetInventoryZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AssetInventory assetInventory,List<AssetInventoryZi> assetInventoryZiList) {
		assetInventoryMapper.updateById(assetInventory);
		
		//1.先删除子表数据
		assetInventoryZiMapper.deleteByMainId(assetInventory.getId());
		
		//2.子表数据重新插入
		if(assetInventoryZiList!=null && assetInventoryZiList.size()>0) {
			for(AssetInventoryZi entity:assetInventoryZiList) {
				//外键设置
				entity.setAssetInventoryOrderH(assetInventory.getId());
				assetInventoryZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		assetInventoryZiMapper.deleteByMainId(id);
		assetInventoryMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			assetInventoryZiMapper.deleteByMainId(id.toString());
			assetInventoryMapper.deleteById(id);
		}
	}
	
}
